#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

int count=0;
class TopicPublisher : public rclcpp::Node
{
public:
    TopicPublisher(std::string name) : Node(name)
    {
        RCLCPP_INFO(this->get_logger(), "节点 %s 已经启动!", name.c_str());
        publisher_ = this->create_publisher<std_msgs::msg::String>("/topic_msg", 10);
        timer_ = this->create_wall_timer(std::chrono::milliseconds(1000), std::bind(&TopicPublisher::timer_callback, this));
    }

private:
    void timer_callback()
    {
        message_.data = "发布,count=" + std::to_string(count) +"!";
        publisher_->publish(message_);
        RCLCPP_INFO(this->get_logger(), "publish: '%s',",message_.data.c_str());
        count++;
    }
    rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
    std_msgs::msg::String message_;
    rclcpp::TimerBase::SharedPtr timer_;
};

int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  auto node = std::make_shared<TopicPublisher>("topic_publisher");
  rclcpp::spin(node);
  rclcpp::shutdown();
  return 0;
}
